package com.xw.echo.core.nacos.client;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;
@Slf4j
public class NacosClientManager {
    private static volatile NamingService namingService;

    private NacosClientManager() {
        // 私有构造函数，防止实例化
    }

    public static NamingService getNamingService(String serverAddr, String username, String password) {
        if (namingService == null) {
            synchronized (NacosClientManager.class) {
                if (namingService == null) {
                    Properties properties = new Properties();
                    properties.put("serverAddr", serverAddr);
                    properties.put("username", username);
                    properties.put("password", password);
                    initializeNamingService(properties);
                }
            }
        }
        return namingService;
    }

    public static NamingService getNamingService(Properties properties) {
        if (namingService == null) {
            synchronized (NacosClientManager.class) {
                if (namingService == null) {
                    initializeNamingService(properties);
                }
            }
        }
        return namingService;
    }

    private static void initializeNamingService(Properties properties) {
        try {
            namingService = NacosFactory.createNamingService(properties);
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
}
